<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Modules - RSS
*  
* This module creates and handles RSS export feeds.
*
* @package		Ineo
* @subpackage   Modules
* @author		Dan Cryer & William McGann
* @copyright	Dan Cryer & William McGann
* @version		1.0.0
*/
class ineo_mod_rss
{
	/**
	* IPSClass Reference - Always passed by the core class on creation.
	*/
	var $ipsclass;

	/**
	* Core class reference.
	*/
	var $core;
	
	function module_init()
	{
		$this->feed_init();
	}
	
	/**
	* Initiate the RSS class, and such.
	*/
	function feed_init()
	{
		require_once( KERNEL_PATH . 'class_rss.php' );
		
		$this->rss = new class_rss();
		
		$this->chan = $this->rss->create_add_channel( array( 'title' => $this->ipsclass->vars['ineo_rss_title'],
				'link' => $this->ipsclass->vars['ineo_rss_link'],
				'description' => $this->ipsclass->vars['ineo_rss_description'],
				'pubDate' => $this->rss->rss_unix_to_rfc( time() ),
				'ttl' => 30 ) );
	}
	
	/**
	* Output the RSS document.
	*/
	function do_output()
	{
		$this->rss->rss_create_document();
		header( 'Content-type: text/xml' );
		print $this->rss->rss_document;
		die();
	}
	
	/**
	* Export newest n items.
	*/
	function recently_added()
	{
		// Pull articles to be displayed.
		$this->ipsclass->DB->simple_construct( array( 'select' => 'a.*,c.*',
				'from'   => array( 'ineo_articles' => 'a' ),
				'add_join' => array( 1 => array( 
						'type' => 'left',
						'from' => array( 'ineo_content' => 'c' ),
						'where' => 'c.content_id = a.article_content' )
						),
				'where' => 'a.article_is_queued = \'0\'' ) );
		
		$this->ipsclass->DB->simple_order( 'a.article_created_date DESC' );
		$this->ipsclass->DB->simple_limit( $this->ipsclass->vars['ineo_rss_outcount'] );									
		$this->ipsclass->DB->simple_exec();
		
		while ( $res = $this->ipsclass->DB->fetch_row() )
		{
			$link = $this->core->libs['global']->make_article_url( $res['article_id'], $res['article_text_id'] );
			$this->rss->create_add_item( $this->chan, array( 'title' => $res['article_title'],
					'link' => $link,
					'description' => $res['article_description'],
					'content' => $res['content_content'],
					'guid' => $this->core->base_url.'showarticle='.$res['article_id'],
					'pubDate' => $this->rss->rss_unix_to_rfc( $res['article_created_date'] ) ) );
		}
		
		$this->do_output();
	}
	
	function cat()
	{
		$catid = intval( $this->ipsclass->input['id'] );
		// Pull articles to be displayed.
		$this->ipsclass->DB->simple_construct( array( 'select' => 'a.*,c.*',
				'from'   => array( 'ineo_articles' => 'a' ),
				'add_join' => array( 1 => array( 
						'type' => 'left',
						'from' => array( 'ineo_content' => 'c' ),
						'where' => 'c.content_id = a.article_content' )
						),
				'where' => 'a.article_is_queued = \'0\' AND a.article_cat_id = \''.$catid.'\'' ) );
		
		$this->ipsclass->DB->simple_order( 'a.article_created_date DESC' );
		$this->ipsclass->DB->simple_limit( $this->ipsclass->vars['ineo_rss_outcount'] );									
		$this->ipsclass->DB->simple_exec();
		
		while ( $res = $this->ipsclass->DB->fetch_row() )
		{
			$link = $this->core->libs['global']->make_article_url( $res['article_id'], $res['article_text_id'] );
			$this->rss->create_add_item( $this->chan, array( 'title' => $res['article_title'],
					'link' => $link,
					'description' => $res['article_description'],
					'content' => $res['content_content'],
					'guid' => $this->core->base_url.'showarticle='.$res['article_id'],
					'pubDate' => $this->rss->rss_unix_to_rfc( $res['article_created_date'] ) ) );
		}
		
		$this->do_output();
	}
	
	function cat_and_subs()
	{
		$catid = intval( $this->ipsclass->input['id'] );
		
		if ( $catid > 0 )
		{
			$this->cat_list = $catid;
			$this->_build_sub_list( $catid );
		}
		
		// Pull articles to be displayed.
		$this->ipsclass->DB->simple_construct( array( 'select' => 'a.*,c.*',
				'from'   => array( 'ineo_articles' => 'a' ),
				'add_join' => array( 1 => array( 
						'type' => 'left',
						'from' => array( 'ineo_content' => 'c' ),
						'where' => 'c.content_id = a.article_content' )
						),
				'where' => 'a.article_is_queued = \'0\' AND a.article_cat_id IN( '.$this->cat_list.' )' ) );
		
		$this->ipsclass->DB->simple_order( 'a.article_created_date DESC' );
		$this->ipsclass->DB->simple_limit( $this->ipsclass->vars['ineo_rss_outcount'] );									
		$this->ipsclass->DB->simple_exec();
		
		while ( $res = $this->ipsclass->DB->fetch_row() )
		{
			$link = $this->core->libs['global']->make_article_url( $res['article_id'], $res['article_text_id'] );
			$this->rss->create_add_item( $this->chan, array( 'title' => $res['article_title'],
					'link' => $link,
					'description' => $res['article_description'],
					'content' => $res['content_content'],
					'guid' => $this->core->base_url.'showarticle='.$res['article_id'],
					'pubDate' => $this->rss->rss_unix_to_rfc( $res['article_created_date'] ) ) );
		}
		
		$this->do_output();
	}
	
	function _build_sub_list( $id )
	{
		foreach ( $this->ipsclass->cache['ineo_cats'][$id]['children'] as $key => $value )
		{
			$this->cat_list .= ','.$key;
			$this->_build_sub_list( $key );
		}
	}
}

?>